package com.leyou.cart.web;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import com.leyou.common.auth.entity.Payload;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.auth.utils.JwtUtils;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加商品到购物车
     * @param cart  购物车对象
     * @return      没有返回值
     */
    @PostMapping
    public ResponseEntity<Void> addToCart(@RequestBody Cart cart){
        cartService.addToCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询当前用户的购物车集合
     * @return  购物车集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> queryCartList(){
        List<Cart> cartList = cartService.queryCartList();
        return ResponseEntity.ok(cartList);
    }

    /**
     * 批量添加购物车信息到redis中
     * @param carts     页面localStorege的购物车信息
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCartList(@RequestBody List<Cart> carts){
        cartService.addCartList(carts);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
